package com.ykdl.member.kid.set;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import oauth.signpost.http.HttpParameters;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.ykdl.member.base.QLog;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.ConsigneeBean;
import com.ykdl.member.kid.beans.ResultBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.set.AddAddressActivity.postDataTag;
import com.ykdl.member.views.MyWheelView;
import com.ykdl.member.views.MyWheelView.OnWheelViewDataChanger;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class EditAddressActivity extends BaseScreen implements OnClickListener{
	private  EditText phone_number,receiver_name,address;
	private MyWheelView location;
	private TextView region ;
	private boolean iopen = false ;
	private ConsigneeBean bean ;
	private Button delt;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("编辑收货地址", 0, R.drawable.back_bg, new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		}, R.string.hmc_bc,
		0, new OnClickListener() {

			@Override
			public void onClick(View v) {
				postData();
			}
		}, R.drawable.title_bg, false);
		bean =(ConsigneeBean) this.getIntent().getSerializableExtra("ConsigneeBean");
		setContent(R.layout.addaddressactivity_xml);
		inint();
	}
	public void inint() {
		delt = (Button)findViewById(R.id.delt);
		delt.setVisibility(View.VISIBLE);
		delt.setOnClickListener(this);
		region = (TextView)findViewById(R.id.region);
		region.setOnClickListener(this);
		receiver_name = (EditText)findViewById(R.id.receiver_name);
		address = (EditText)findViewById(R.id.address);
		phone_number = (EditText)findViewById(R.id.phone_number);
		phone_number.setInputType(EditorInfo.TYPE_CLASS_PHONE);
		location = (MyWheelView) findViewById(R.id.select_location_layout);
		location.setVisibility(View.INVISIBLE);
		location.setWheelView(MyWheelView.CHINA);
		location.setOnWheelViewDataChanger(new OnWheelViewDataChanger() {
			@Override
			public void updataByChanger(String data) {
				
				QLog.debug("data", data);
				String[] datas = data.split("&#&");
				String[] contents = datas[0].split(",");
				String result = "";
				for (int i = 0; i < contents.length; i++) {
					if (!"".equals(result)) {
						result += ",";
					}
					result += contents[i];
				}
				region.setText(result);
			
				}
		});
		setData(bean);
	}
	public void setData(ConsigneeBean bean) {
		receiver_name.setText(bean.getName());
		region.setText(bean.getArea());
		phone_number.setText(bean.getPhone());
		address.setText(bean.getAddress());
	}
	public void postData() {
		
		String  pageurl =KidConfig.EDIT_CONSIGNEE;
		if (receiver_name.getText().toString().trim().length()==0) {
			Toast.makeText(mContext, "请填写收货人姓名", 1).show();
			return;
		}
		if (phone_number.getText().toString().trim().length()!=11) {
			Toast.makeText(mContext, "您输入的手机号错误,请输入11位手机号", 1).show();
			return;
		}
		if (region.getText().toString().trim().length()==0) {
			Toast.makeText(mContext, "请选择所在区域", 1).show();
			return;
		}
		if (address.getText().toString().trim().length()==0) {
			Toast.makeText(mContext, "请详细的地址,如街道,门牌号等", 1).show();
			return;
		}
		showProgress("保存中请稍等...");
		String name="";
		try {
			name = URLEncoder.encode(receiver_name.getText().toString().trim(), "utf8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String area="";
		try {
			area = URLEncoder.encode(region.getText().toString().trim(), "utf8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String addresss="";
		try {
			addresss = URLEncoder.encode(address.getText().toString().trim(), "utf8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String phone_numbers="";
		try {
			phone_numbers = URLEncoder.encode(phone_number.getText().toString().trim(), "utf8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpParameters parameterss = new HttpParameters();
		parameterss.put("name", name);
		parameterss.put("phone",phone_numbers);
		parameterss.put("area", area);
		parameterss.put("address", addresss);
		parameterss.put("consignee_id", bean.getConsignee_id());
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl,parameterss,null);
		 TaskManager.startHttpRequest(request, new postDataTag(),  ResultBean.class);
	}
	
	class postDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof ResultBean) {
				ResultBean bean = (ResultBean)t;
				if (null==bean.getError()) {
					Toast.makeText(mContext, "添加成功", 1).show();
					EditAddressActivity.this.finish();
				}else {
					Toast.makeText(mContext, "添加失败,"+bean.getDesc(), 1).show();
				}
				}
				
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.region:
			location.setVisibility(View.VISIBLE);
			((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
					.hideSoftInputFromWindow(EditAddressActivity.this
							.getCurrentFocus().getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);
			location.setInVisible(true);
			location.setMyVisible();
			break;
		case R.id.delt:
			deltData();
			break;
		default:
			break;
		}
	}
	public void deltData() {
		showProgress("请稍等...");
		String  pageurl =KidConfig.DELETE_CONSIGNEE;
		HttpParameters parameterss = new HttpParameters();
		parameterss.put("consignee_id", bean.getConsignee_id());
		HttpBaseRequest request  =  Wxxr.getInstance().getRequest(pageurl,parameterss);
		 TaskManager.startHttpRequest(request, new deltDataTag(),  ResultBean.class);
	}
	
	class deltDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof ResultBean) {
				ResultBean bean = (ResultBean)t;
				if (null==bean.getError()) {
					Toast.makeText(mContext, "删除成功", 1).show();
					EditAddressActivity.this.finish();
				}else {
					Toast.makeText(mContext, "删除失败,"+bean.getDesc(), 1).show();
				}
				}
				
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	}
